package com.MusicWeb.servlet;

import com.MusicWeb.entity.User;
import com.MusicWeb.entity.UserInformation;
import com.MusicWeb.service.UserService;
import com.MusicWeb.service.UserinformationService;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/token")
//todo: 根据token 获取数据库用户信息表中的所有信息
//http://localhost:8080/token
public class TokenServlet extends HttpServlet {
    UserinformationService userinformationService=new UserinformationService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=utf-8");

        String content=req.getParameter("content");

        Cookie[] cookies = req.getCookies();
        String tokenValue = "";
        if (cookies != null) {
            for (int i = 0; i < cookies.length; i++) {
                if (cookies[i].getName().equals("token")) {
                    tokenValue = cookies[i].getValue();
                }
            }
        }
        UserService userService = new UserService();
        User user = null;
        user = userService.Selecttoken(tokenValue);
        UserInformation userInformation=userinformationService.select(user.getId());
        if (userInformation!=null) {
            // 设置返回的数据为json对象
            // 把实体转换为json，返回给前端
            String str = new Gson().toJson(userInformation);
            resp.getWriter().print(str);
        }


    }
}
